<template>
  <el-dialog :title="!dataForm.id ? '新增' : '修改'" :close-on-click-modal="false" :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
      <el-form-item label="门店名称" prop="storeName">
        <el-input v-model="dataForm.storeName" placeholder="门店名称"></el-input>
      </el-form-item>
      <el-form-item label="活动名称" prop="activityName">
        <el-input v-model="dataForm.activityName" placeholder="活动名称"></el-input>
      </el-form-item>
      <el-form-item label="分享者名称" prop="shareName">
        <el-input v-model="dataForm.shareName" placeholder="分享者名称"></el-input>
      </el-form-item>
      <el-form-item label="分享者头像" prop="shareHeadUrl">
        <img :src="dataForm.shareHeadUrl" alt="分享者头像" >
      </el-form-item>
      <el-form-item label="手机号" prop="mobile">
        <el-input v-model="dataForm.mobile" placeholder="手机号"></el-input>
      </el-form-item>
      <el-form-item label="创建时间" prop="createTime">
        <el-input v-model="dataForm.createTime" placeholder="创建时间"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data() {
      return {
        visible: false,
        dataForm: {
          id: 0,
          storeId: '',
          storeName: '',
          activityId: '',
          activityName: '',
          shareName: '',
          shareHeadUrl: '',
          mobile: '',
          createTime: ''
        },
        dataRule: {
          storeId: [
            { required: true, message: '门店id不能为空', trigger: 'blur' }
          ],
          storeName: [
            { required: true, message: '门店名称不能为空', trigger: 'blur' }
          ],
          activityId: [
            { required: true, message: '活动id不能为空', trigger: 'blur' }
          ],
          activityName: [
            { required: true, message: '活动名称不能为空', trigger: 'blur' }
          ],
          shareName: [
            { required: true, message: '分享者名称不能为空', trigger: 'blur' }
          ],
          shareHeadUrl: [
            { required: true, message: '分享者头像不能为空', trigger: 'blur' }
          ],
          mobile: [
            { required: true, message: '手机号不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init(id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/activity/share/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({ data }) => {
              if (data && data.code === 0) {
                this.dataForm.storeId = data.share.storeId
                this.dataForm.storeName = data.share.storeName
                this.dataForm.activityId = data.share.activityId
                this.dataForm.activityName = data.share.activityName
                this.dataForm.shareName = data.share.shareName
                this.dataForm.shareHeadUrl = data.share.shareHeadUrl
                this.dataForm.mobile = data.share.mobile
                this.dataForm.createTime = data.share.createTime
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit() {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/activity/share/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'storeId': this.dataForm.storeId,
                'storeName': this.dataForm.storeName,
                'activityId': this.dataForm.activityId,
                'activityName': this.dataForm.activityName,
                'shareName': this.dataForm.shareName,
                'shareHeadUrl': this.dataForm.shareHeadUrl,
                'mobile': this.dataForm.mobile,
                'createTime': this.dataForm.createTime
              })
            }).then(({ data }) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
